package com.sunchangming.stmdb;

import org.apache.log4j.BasicConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StmDB {
	private final static Logger logger = LoggerFactory.getLogger(StmDB.class);

	static private volatile StmDB instance;

	public static StmDB getInstance() {
		if (instance == null) {
			synchronized (StmDB.class) {
				if (instance == null)
					instance = new StmDB();
			}
		}
		return instance;
	}

	private StmDB() {
		BasicConfigurator.configure();
		logger.info("start StmDB");
		try {
			ModuleManager.getInstance().init(null);
		} catch (final Exception ex) {
			logger.error("init fail", ex);
			ModuleManager.getInstance().fini();
		}
		IDGenerator.getInstance().setServerid(1);
		//TODO: register types to IDGenerator. every auto increment table should have one 
	}

	public void shutdown() {
		logger.info("shutdown StmDB begin");
		ModuleManager.getInstance().fini();
		logger.info("shutdown StmDB end");
	}
}
